<?php
namespace Catalog\Data;

use Catalog\Entity\LocalizedCategoryText;
/**
 *
 * @author Paolo Bertuzzo
 *        
 */
class ProductDao extends AbstractDao
{

    /**
     */
    public function get($productId)
    {
        $query = $this->entityManager->createQuery('select p from Catalog\Entity\Product p where p.productId = ?1');
        
        $query->setParameter(1, $productId);
        $query->useResultCache(true);

        $result = $query->getResult();
        
        if (count($result) == 0)
            $this->logger->info(sprintf('Product not found for id %d', $productId));

        return count($result) == 0 ? null : $result[0];
    }

    /**
     * 
     * @param string $language
     * @param integer $pageSize
     * @param integer $storeId
     */
    public function getNew($language, $pageSize, $storeId)
    {
        $query = $this->entityManager->createQuery('select p, lpt, pi, pp from Catalog\Entity\Product p, Catalog\Entity\LocalizedProductText lpt, Catalog\Entity\ProductImage pi, Catalog\Entity\ProductPrice pp ' .
            ' where p.isNew = true and (lpt.type = \'name\' or lpt.type = \'alias\') and lpt.language = ?1 and p.productId = lpt.productId and p.productId = pi.productId and pi.type = \'default\' and pi.language = ?2 and p.productId = pp.productId and pp.storeId = ?3');
        
        $query->setParameter(1, $language);
        $query->setParameter(2, $language);
        $query->setParameter(3, $storeId);
        
        $query->setMaxResults($pageSize);        
        $query->useResultCache(true);
        
        return $query->getResult();
    }
    
}

?>